1925D - Good Trip - CodeForces Solution


combinatorics math probabilities

Please click on ads to support us..

Python Code:

import sys
sys.setrecursionlimit(10000000)

m = 1000000007
 
def gcd(a, b):
 
    if (a == 0):
        return b
    return gcd(b % a, a)
 
def modexp(x, n):
 
    if (n == 0) :
        return 1
     
    elif (n % 2 == 0) :
        return modexp((x * x) % m, n // 2)
     
    else :
        return (x * modexp((x * x) % m, 
                           (n - 1) / 2) % m)
 
 
def getFractionModulo(a, b):
 
    c = gcd(a, b)
 
    a = a // c
    b = b // c
 
        d = modexp(b, m - 2)
 
        ans = ((a % m) * (d % m)) % m
 
    return ans

t = int(input())
for _ in range(t):
    n, _m, k = map(int, input().split())
    sm = 0
    for __ in range(_m):
        _a, _b, x = map(int, input().split())
        sm += x
    if not _m: 
        print(0)
        continue

    p = 2 * sm * k * (n**2 - n) + 2 * _m * (k**2 - k)
    q = n ** 4 + n ** 2 - 2 * (n ** 3)
    print(getFractionModulo(p, q))

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef tree<pair<int, int>, null_type, greater<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define AboTaha_on_da_code ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define X first
#define Y second

const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1};
const int dy[8]={1, -1, 0, 0, 1, -1, 1, -1};
const int mod = 1e9+7; // 1e9+7, 998244353
const double EPS = 1e-8;
// BEFORE coding are you sure you understood the statement correctly?
// PLEASE do not forget to read the sample explanation carefully.
// WATCH out for overflows & RTs in general.
// TEST your idea or code on the corner cases.
// ANALYZE each idea you have thoroughly.

int mpow(int a, int b, int m)
{
    int res = 1;
    while(b) {
        if (b&1) res = 1LL*res*a%m;
        a = 1LL*a*a%m, b/=2;
    }
    return res;
}

const int N = 2e5+10;
int fact[N], invfact[N];
void pre()
{
    fact[0] = invfact[0] = 1;
    for (int i = 1; i < N; i++) {
        fact[i] = 1LL*fact[i-1]*i%mod;
        invfact[i] = mpow(fact[i], mod-2, mod);
    }
}

int nCr(int n, int r)
{
    if (r > n) return 0;
    return 1LL*fact[n]*invfact[r]%mod*invfact[n-r]%mod;
}

inline int inv(int x) {
    return mpow(x, mod-2, mod);
}

void burn(int tc) {
    int n, m, k; cin >> n >> m >> k;
    ll sm = 0;
    for (int i = 0; i < m; i++) {
        int x; cin >> x >> x >> x;
        sm+=x;
        if (sm >= mod) sm-=mod;
    }
    int ans = 0;
    for (int x = 0; x < k; x++) {
        int cur = 1LL*(x+1)*(2*sm+x)/2%mod*inv(m)%mod;
        cur = 1LL*cur*mpow(1LL*m*inv(nCr(n, 2))%mod, x+1, mod)%mod;
        cur = 1LL*cur*mpow((mod+1-1LL*m*inv(nCr(n, 2))%mod)%mod, k-x-1, mod)%mod;
        cur = 1LL*cur*nCr(k, x+1)%mod;
        ans+=cur;
        if (ans >= mod) ans-=mod;
    }
    cout << ans;
}

int main() {

    AboTaha_on_da_code

//    freopen("khoshaf.in", "r", stdin);
//    freopen("Aout.txt", "w", stdout);

    int _t = 1; cin >> _t;

    pre();

    for (int i = 1; i <= _t; i++) {
//        cout << "Case " << i << ": ";
        burn(i);
        cout << '\n';
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating